package com.ruoyi.commodity.service.impl;

import com.ruoyi.commodity.mapper.BrandMapper;
import com.ruoyi.commodity.service.BrandService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.AuthBrand;
import com.ruoyi.common.core.domain.Brand;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;
    @Override
    @DataScope(BrandAlias = "b")
    public List<Brand> selectList(Brand brand) {
        return brandMapper.selectList(brand);
    }

    @Override
    public List<TreeSelect> selectBrandTreeList(Brand brand) {
        List<Brand> brandList = SpringUtils.getAopProxy(this).selectList(brand);
        return buildBrandTreeSelect(brandList);
    }

    @Override
    public List<Brand> buildBrandTree(List<Brand> brandList) {
        List<Brand> returnList = new ArrayList<>();
        List<Long> collect = brandList.stream().map(Brand::getId).collect(Collectors.toList());
        for (Brand brand : brandList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(brand.getParentId())){
                recursionFn(brandList,brand);
                returnList.add(brand);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = brandList;
        }
        return returnList;
    }

    /**
     * 递归列表
     * @param brandList 子级
     * @param brand 父级
     */
    private void recursionFn(List<Brand> brandList , Brand brand){
        // 得到子节点列表
        List<Brand> childList = getChildList(brandList, brand);
        brand.setChildrenBrandForm(childList);
        for (Brand newBrand : childList)
        {
            if (hasChild(brandList, newBrand))
            {
                recursionFn(brandList, newBrand);
            }
        }
    }

    /**
     * 获得子节点
     * @param brandList
     * @param brand
     * @return
     */
    private List<Brand> getChildList(List<Brand> brandList, Brand brand){
        List<Brand> returnList = new ArrayList<>();
        Iterator<Brand> iterator = brandList.iterator();
        while (iterator.hasNext()){
            Brand n = (Brand) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == brand.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Brand> brandList, Brand brand)
    {
        return getChildList(brandList, brand).size() > 0;
    }

    @Override
    public List<TreeSelect> buildBrandTreeSelect(List<Brand> brandList) {
        List<Brand> brandTree = buildBrandTree(brandList);
        return brandTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public Brand selectById(Long id) {
        return brandMapper.selectById(id);
    }
}
